<?php

namespace app\api\controller\kuyou\applet;

use think\Db;

class Revenue extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $purchase_time = $this->request->post('purchase_time');
            if(!empty($purchase_time)){
                $str = explode('~',$purchase_time);
                if(isset($str[0]) && isset($str[1])){
                    $where['i.purchase_time'] = ["BETWEEN", [strtotime($str[0]),strtotime($str[1])]];
                }
            }
            $business_type = $this->request->post('business_type');

            $field = empty(input('field'))?'purchase_time':trim(input('field'));
            $order = empty(input('sort'))?'ASC':trim(input('sort'));

            $lists = [];
            if(!isset($business_type) || (empty($business_type) || (isset($business_type) && $business_type==1))){
                $list = Db::name('kuyou_income_info')->alias('i')
                    ->join('kuyou_customers c','c.id=i.customers_id')
                    ->join('kuyou_income_type t','t.id=i.income_type_id')
                    ->where($where)
                    ->order([$field=>$order])
                    ->field('i.purchase_code,i.purchase_time,i.money,i.remark,c.customer_name,t.name')
                    ->select();
                foreach ($list as $list_k=>$list_v){
                    $lists[] = [
                        'purchase_time' => !empty($list_v['purchase_time'])?date('Y-m-d',$list_v['purchase_time']):'',
                        'purchase_code' => $list_v['purchase_code'],
                        'business_type' => '其他收入',
                        'name'          => $list_v['name'],
                        'receivable'    => $list_v['money'],
                        'amount'        => '',
                        'intercourse'   => $list_v['customer_name'],
                        'remark'        => $list_v['remark'],
                    ];
                }
            }

            if(!isset($business_type) || (empty($business_type) || (isset($business_type) && $business_type==2))){
                $list = Db::name('kuyou_expenditure_info')->alias('i')
                    ->join('kuyou_suppliers c','c.id=i.suppliers_id')
                    ->join('kuyou_expend_type t','t.id=i.expend_type_id')
                    ->where($where)
                    ->order([$field=>$order])
                    ->field('i.purchase_code,i.purchase_time,i.money,i.remark,c.supplyer_name,t.name')
                    ->select();
                foreach ($list as $list_k=>$list_v){
                    $lists[] = [
                        'purchase_time' => !empty($list_v['purchase_time'])?date('Y-m-d',$list_v['purchase_time']):'',
                        'purchase_code' => $list_v['purchase_code'],
                        'business_type' => '其他支出',
                        'name'          => $list_v['name'],
                        'receivable'    => '',
                        'amount'        => $list_v['money'],
                        'intercourse'   => $list_v['supplyer_name'],
                        'remark'        => $list_v['remark'],
                    ];
                }
            }

            $result = array("count" => sizeof($lists), "list" => $lists);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
